<?php

namespace App\Admin\Extensions\Actions;

use Encore\Admin\Admin;

class RefundAction
{
    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function script()
    {
        return <<<SCRIPT
$('.grid-row-handle-withdraw').unbind('click').click(function() {

    var id = $(this).data('id');

    swal({
        title: "确认退款处理?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        showLoaderOnConfirm: true,
        cancelButtonText: "取消",
        preConfirm: function() {
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '/admin/arenacontestuser/' + id + '/refundorder',
                    data: {
                        _method:'post',
                        _token:LA.token,
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');

                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
                swal(data.message, '', 'success');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        return "<a href='javascript:void(0);' data-id='{$this->id}' class='grid-row-handle-withdraw'>
                    <i class='fa fa-gavel'></i>
                </a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}
